<template>
  <div class="main_box">
    <CommonHeader title="红/绿牌占比排名">
      <div class="down_box" @click="showArea">
        <div class="tit">{{ selectName }}</div>
        <i class="icon"></i>
      </div>
    </CommonHeader>
    <div class="nav_box">
      <div class="nav_content">
        <div class="nav_item" :class="item.value == mode ? 'active' : ''" v-for="item in tabList" :key="item.value" @click="changeCard(item)">
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="main_content">
      <div class="nav_wrap" style="font-size: 14px">
        <van-radio-group direction="horizontal" v-model="state" icon-size="14">
          <van-radio :name="item.value" v-for="item in selectList" :key="item.value" @click="changeNav(item)">{{ item.text }}</van-radio>
        </van-radio-group>
      </div>
      <div class="nav_wrap">
        <div class="tit" style="width: 156px" v-if="$route.query.level == 2 && selectName == '人员'">辖区/负责人：</div>
        <div class="content" style="width: 180px" @click="showPersonSelect" v-if="$route.query.level == 2 && selectName == '人员'">
          <div style="max-width: 80px">{{ selectArea }}</div>
          <i class="down"></i>
        </div>
        <div class="tit">选择时间：</div>
        <div class="content" @click="showSelectDate">
          <div>{{ currentDate }}</div>
          <i class="down"></i>
        </div>
      </div>
      <div class="main_list" v-if="mode == '1'">
        <div v-for="item in dataList" :key="item.index" class="quota_item">
          <div class="head">
            <div class="tit" v-show="selectName == '人员'">负责领导：{{ item.empName }}</div>
            <div class="tit" v-show="selectName == '县（市、区）'">
              {{ item.areaName }}
            </div>
            <div class="red_per">
              红牌占比
              <span class="red">{{ item.redPro ? item.redPro + '%' : '0.0%' }}</span>
            </div>
          </div>
          <div class="footer">
            <div>指标数：{{ item.quotaCount }}</div>
            <div>红牌数：{{ item.redCount }}</div>
          </div>
        </div>
      </div>
      <div class="main_list" v-else>
        <div v-for="item in dataList" :key="item.index" class="quota_item">
          <div class="head">
            <div class="tit" v-show="selectName == '人员'">负责领导：{{ item.empName }}</div>
            <div class="tit" v-show="selectName == '县（市、区）'">
              {{ item.areaName }}
            </div>
            <div class="red_per">
              绿牌占比
              <span class="green">{{ item.greenPro ? item.greenPro + '%' : '0.0%' }}</span>
            </div>
          </div>
          <div class="footer">
            <div>指标数：{{ item.quotaCount }}</div>
            <div>绿牌数：{{ item.greenCount }}</div>
          </div>
        </div>
      </div>
    </div>
    <van-popup v-model="dateShow" round position="bottom" get-container="#app">
      <van-datetime-picker type="year-month" :value="now" @confirm="onConfirm" @cancel="cancel" :min-date="new Date('2022/1/1')" />
    </van-popup>
    <van-popup v-model="personShow" round position="bottom" get-container="#app">
      <van-picker show-toolbar :columns="personList" confirm-button-text="确定" cancel-button-text="取消" @confirm="confirmPerson" @cancel="canclePerson" />
    </van-popup>
    <van-popup v-model="selectShow" round position="bottom" get-container="#app">
      <van-picker show-toolbar :columns="areaSelect" confirm-button-text="确定" cancel-button-text="取消" @confirm="confirmSelect" @cancel="cancleSelect" />
    </van-popup>
  </div>
</template>

<script>
import CommonHeader from '@/components/commonHeader';
import { Toast } from 'vant';
export default {
  // eslint-disable-next-line
  name: 'RedCard',
  components: {
    CommonHeader,
  },
  data() {
    return {
      tabList: [
        {
          text: '红牌',
          value: '1',
        },
        {
          text: '绿牌',
          value: '2',
        },
      ],
      selectList: [
        {
          text: '月度(季度)',
          value: '1',
        },
        // {
        //   text: '年度',
        //   value: '2',
        // },
      ],
      areaSelect: ['人员', '县（市、区）'],
      selectName: '人员',
      selectShow: false,
      state: '1',
      mode: '1',
      dateShow: false,
      now: '',
      currentDate: '',
      personShow: false,
      personList: [],
      selectArea: '鹿城区',
      employId: '',
      areaId: '',
      employName: '全部',
      dataList: [], // 列表数据实体
    };
  },
  watch: {
    areaId: {
      handler: function (val) {
        if (val) {
          this.initData();
        }
      },
      immediate: true,
    },
  },
  computed: {
    formateSignage() {
      return function (id) {
        switch (id) {
          case 0:
            return 'green';
          case 1:
            return 'red';
          default:
            return '';
        }
      };
    },
  },
  created() {
    const year = this.$route.query.year;
    const month = this.$route.query.month;
    this.currentDate = year + '-' + month;
    this.now = new Date(this.currentDate);
    this.initData();
    if (this.$route.query.level == 2) {
      this.getAreaPersonList();
    }
  },
  methods: {
    changeCard(data) {
      this.mode = data.value;
      this.initData();
    },
    changeNav(data) {
      if (data.value === '2') {
        Toast.fail('该功能暂未开放！');
        this.state = '1';
        return;
      }
      this.initData();
    },
    showSelectDate() {
      this.dateShow = true;
    },
    showPersonSelect() {
      this.personShow = true;
    },
    onConfirm(params) {
      const year = params.getFullYear();
      const month = params.getMonth() + 1 > 9 ? params.getMonth() + 1 : '0' + (params.getMonth() + 1);
      this.currentDate = year + '-' + month;
      this.dateShow = false;
      this.initData();
    },
    cancel() {
      this.dateShow = false;
    },
    initData() {
      let params = {};
      let date = '';
      if (this.currentDate && this.currentDate != '-') {
        const arr = this.currentDate.split('-');
        const year = arr[0];
        const month = arr[1] / 1;
        date = year + '-' + month;
      }
      if (this.selectName == '县（市、区）') {
        const _mt = this.mode == '1' ? 'cityRedTrend' : 'cityGreenTrend';
        params = {
          _gp: 'quota',
          _mt: _mt,
          issueNo: date,
          page: 1,
          limit: 99,
        };
      } else {
        const _mt = this.mode == '1' ? 'redTrend' : 'greenTrend';
        params = {
          _gp: 'quota',
          _mt: _mt,
          areaId: this.areaId || sessionStorage.getItem('areaId'),
          employId: this.employId,
          issueNo: date,
          page: 1,
          limit: 99,
        };
      }
      this.$api.commonApi(params).then((res) => {
        if (res.errno === 200) {
          const rows = res.data.items || [];
          this.dataList = rows;
        } else {
          this.dataList = [];
        }
      });
    },
    getAreaPersonList() {
      const params = {
        _gp: 'employee',
        _mt: 'getEmpByArea',
      };
      this.$api.commonApi(params).then((res) => {
        if (res.errno == 200) {
          this.personList = res.data.map((item) => {
            const arr = item.employeeDOList.map((desc) => {
              return {
                value: desc.empId,
                text: desc.name,
              };
            });
            return {
              value: item.areaId,
              text: item.areaName,
              children: [
                {
                  text: '全部',
                  value: '',
                },
                ...arr,
              ],
            };
          });
          this.areaId = this.personList[0].value;
        }
      });
    },
    confirmPerson(params, inx) {
      this.areaId = this.personList[inx[0]].value;
      this.employId = this.personList[inx[0]].children[inx[1]].value;
      if (params[1] == '全部') {
        this.selectArea = params[0];
      } else {
        this.selectArea = params[0] + '-' + params[1];
      }
      this.personShow = false;
      this.initData();
    },
    canclePerson() {
      this.personShow = false;
    },
    showArea() {
      this.selectShow = true;
    },
    confirmSelect(params) {
      this.selectName = params;
      this.selectShow = false;
      this.initData();
    },
    cancleSelect() {
      this.selectShow = false;
    },
  },
};
</script>

<style scoped>
.main_box {
  min-height: 100vh;
  background-color: #f4f5fa;
}
.nav_box {
  width: 100%;
  background-color: #fff;
}
.nav_content {
  width: 100%;
  display: flex;
}
.nav_content .nav_item {
  width: 49%;
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #999;
  background-color: #fff;
  cursor: pointer;
  position: relative;
  box-sizing: border-box;
}
.nav_content .nav_item.active {
  color: #333;
}
.nav_content .nav_item.active::after {
  display: block;
  content: '';
  width: 50px;
  border-bottom: 2px solid #1b68eb;
  position: absolute;
  left: 50%;
  bottom: 0;
  margin-left: -25px;
}
.main_list {
  width: 100%;
  padding: 10px 15px;
  box-sizing: border-box;
}
.main_content {
  padding: 15px;
}

.nav_wrap {
  width: 100%;
  display: flex;
  margin-bottom: 15px;
}
.nav_wrap .tit {
  width: 120px;
  font-size: 13px;
  color: #999;
}
.nav_wrap .content {
  width: 120px;
  font-size: 13px;
  color: #333;
  display: flex;
  line-height: 18px;
}
.nav_wrap .content .down {
  width: 15px;
  height: 8px;
  background: url('../../assets/image/down.png') no-repeat;
  background-size: 100% 100%;
  position: relative;
  left: 10px;
  top: 5px;
}
.quota_item {
  width: 100%;
  background-color: #fff;
  border-radius: 8px;
  margin-bottom: 10px;
  text-align: left;
  padding: 10px 15px;
  box-sizing: border-box;
}
.quota_item .head {
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  border-bottom: 1px solid #efefef;
  padding-bottom: 10px;
}
.quota_item .head .tit {
  max-width: 200px;
  height: 20px;
  font-size: 14px;
  color: #333;
  font-weight: bold;
  border-left: 4px solid #367fff;
  padding-left: 10px;
  box-sizing: border-box;
}
.quota_item .red_per {
  font-size: 13px;
  color: #bdc0c7;
}
.quota_item .red_per .red {
  display: inline-block;
  width: 60px;
  text-align: right;
  font-size: 15px;
  color: #e43c26;
}
.quota_item .red_per .green {
  display: inline-block;
  width: 60px;
  text-align: right;
  font-size: 15px;
  color: #5bc55a;
}
.quota_item .footer {
  display: flex;
  margin-top: 10px;
  font-size: 13px;
  color: #bcc0c7;
  padding-top: 10px;
}
.quota_item .footer div {
  margin-right: 20px;
}
.down_box {
  font-size: 13px;
  color: #666;
  position: absolute;
  right: 15px;
  top: 13px;
  display: flex;
}
.down_box .icon {
  display: inline-block;
  width: 15px;
  height: 8px;
  background: url('../../assets/image/down.png') no-repeat;
  background-size: 100% 100%;
  margin-left: 5px;
  position: relative;
  top: 6px;
}
</style>
